package com.mjl.shops.activity;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.mjl.shops.R;
import com.mjl.shops.base.BaseActivity;
import com.mjl.shops.base.BaseDate;
import com.mjl.shops.utils.CommUtils;
import com.mjl.shops.utils.SharedUtils;
import com.mjl.shops.utils.StringUtils;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchActivity extends BaseActivity {

    private static final String TAG = "SearchActivity";

    private List<String> hotArray = new ArrayList<>();

    private List<String> history = new ArrayList<>();

    private Gson gson;

    @BindView(R.id.history_search_flow)
    TagFlowLayout history_search_flow;

    @BindView(R.id.hot_flow)
    TagFlowLayout mHotTag;

    @BindView(R.id.search_et)
    EditText search_et;

    @BindView(R.id.history_ll)
    LinearLayout history_ll;

    @Override
    protected void setContentViewBefore() {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    @Override
    protected int getViews() {
        return R.layout.activity_search;
    }

    @Override
    protected void setViews() {
        ButterKnife.bind(this);
        gson = new Gson();
    }

    @Override
    protected void setDate() {

        history.clear();
        String hisStr = SharedUtils.getInstance().getString(CommUtils.HISTORY,"");
        history.addAll(BaseDate.getHistory(hisStr));
        Collections.reverse(history);
        if (history!=null&&history.size()>0){
            history_ll.setVisibility(View.VISIBLE);
            history_search_flow.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                @Override
                public boolean onTagClick(View view, int position, FlowLayout parent) {
                    if (history!=null&& StringUtils.strEmpty(history.get(position))){
                        search_et.setText(history.get(position));
                        search_et.setSelection(search_et.getText().toString().trim().length());
                        SearchSucceedActivity.StartIntent(SearchActivity.this,search_et.getText().toString().trim());
                        finish();
                    }
                    return true;
                }
            });
        }else{
            history_ll.setVisibility(View.GONE);
        }

        history_search_flow.setAdapter(new TagAdapter(history) {
            @Override
            public View getView(FlowLayout parent, int position, Object o) {
                View tagView = LayoutInflater.from(SearchActivity.this).inflate(R.layout.item_str_layout,null,false);
                TextView textView = tagView.findViewById(R.id.tag_text);
                if (history!=null&& StringUtils.strEmpty(history.get(position))){
                    textView.setText(history.get(position));
                }else{
                    textView.setText("未知");
                }
                return tagView;
            }
        });

        hotArray.clear();
        hotArray.addAll(BaseDate.getStr());
        mHotTag.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                if (hotArray!=null&& StringUtils.strEmpty(hotArray.get(position))){
                    search_et.setText(hotArray.get(position));
                    search_et.setSelection(search_et.getText().toString().trim().length());
                    //保存搜索内容
                    if (history.size()>9){
                        history.remove(history.size()-1);
                    }
                    history.add(hotArray.get(position));
                    SharedUtils.getInstance().setString(CommUtils.HISTORY,gson.toJson(history));
                    SearchSucceedActivity.StartIntent(SearchActivity.this,search_et.getText().toString().trim());
                    finish();
                }
                return true;
            }
        });
        mHotTag.setAdapter(new TagAdapter(hotArray) {
            @Override
            public View getView(FlowLayout parent, int position, Object o) {
                View tagView = LayoutInflater.from(SearchActivity.this).inflate(R.layout.item_str_layout,null,false);
                TextView textView = tagView.findViewById(R.id.tag_text);
                if (hotArray!=null&& StringUtils.strEmpty(hotArray.get(position))){
                    textView.setText(hotArray.get(position));
                }else{
                    textView.setText("未知");
                }
                return tagView;
            }
        });

        search_et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH){
                    String search_str = search_et.getText().toString().trim();
                    if (StringUtils.strEmpty(search_str)){
                        SearchSucceedActivity.StartIntent(SearchActivity.this,search_str);
                        finish();
                        return true;
                    }else{
                        CommUtils.setToast("请输入搜索内容！");
                        return false;
                    }
                }
                return false;
            }
        });
    }

    @Override
    protected String getStateColor() {
        return CommUtils.color_05000000;
    }

    @Override
    protected int getStetaBackgroundStyle() {
        return 1;
    }

    public static void StartIntent(Context contexts){
        Intent intent = new Intent(contexts,SearchActivity.class);
        contexts.startActivity(intent);
    }
}
